﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Configuration;
using Copad.BusinessCard.Model;
using System.Data.Entity.ModelConfiguration;

namespace Copad.BusinessCard.DAL
{
    public class Entities : DbContext
    {
        public Entities()
            : base(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString.Replace("{AppDir}", AppDomain.CurrentDomain.BaseDirectory))
        {

        }

        public IDbSet<Log> Loges { get; set; }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageIndex">页索引，从1开始</param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <param name="whereFunc"></param>
        /// <param name="orderFunc"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetPagedEntities<T, S>(int pageIndex, int pageSize, out int recordCount, Func<T, S> orderFunc, params Func<T, bool>[] whereFunc) where T : class
        {
            return GetPagedEntities<T, S>(pageIndex, pageSize, out recordCount, orderFunc, true, whereFunc);
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <param name="pageIndex">页索引，从1开始</param>
        /// <param name="pageSize"></param>
        /// <param name="recordCount"></param>
        /// <param name="whereFunc"></param>
        /// <param name="orderFunc"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetPagedEntities<T, S>(int pageIndex, int pageSize, out int recordCount, Func<T, S> orderFunc, bool isAsc, params Func<T, bool>[] whereFunc) where T : class
        {
            IEnumerable<T> query = Set<T>();
            if (whereFunc != null)
            {
                foreach (var item in whereFunc)
                {
                    query = query.Where(item);
                }
            }

            recordCount = query.Count();
            if (isAsc)
            {
                query = query.OrderBy(orderFunc);
            }
            else
            {
                query = query.OrderByDescending(orderFunc);
            }
            query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
            return query.AsQueryable();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add<Log>(new EntityTypeConfiguration<Log>());
            modelBuilder.Entity<Log>().ToTable("Log");
            modelBuilder.Entity<Log>().HasKey(c => c.ID);
            modelBuilder.Entity<Log>().Ignore(x => x.Operation);
            modelBuilder.Entity<Log>().Property(x => x.OperationValue).HasColumnName("Operation");
            base.OnModelCreating(modelBuilder);
        }
    }
}
